package com.lieve.tx.typeinfo;

import com.lieve.tx.typeinfo.factory.Factory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author sunyoboy
 * 2020/9/25
 */
public class RegisteredFactories {
    static class Part {
        @Override
        public String toString() {
            return getClass().getSimpleName();
        }
        static List<Factory<? extends Part>> partFactories =
                new ArrayList<>();

        static {
            partFactories.add(new FuelFilter.Factory());
        }
        private static Random rand = new Random(47);
        public static Part createRandom() {
            int n = rand.nextInt(partFactories.size());
            return partFactories.get(n).create();
        }

    }



    static class Filter extends Part {}

    static class FuelFilter extends Filter {
        static class Factory implements com.lieve.tx.typeinfo.factory.Factory<FuelFilter> {
            @Override
            public FuelFilter create() {
                return new FuelFilter();
            }
        }
    }

    public static void main(String[] args) {
        for(int i = 0; i < 10; i++) {
            System.out.println(Part.createRandom());
        }
    }
}
